package cn.stylefeng.guns.czlm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.czlm.entity.TbCoupon;
import cn.stylefeng.guns.czlm.warpper.TbCouponWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.*;


@Controller
@RequestMapping("/coupon")
public class TbCouponController extends BaseController {

    private static String PREFIX = "/modular/system/coupon/";

    @Autowired
    private cn.stylefeng.guns.czlm.service.TbCouponService couponService;

    @Autowired
    private cn.stylefeng.guns.czlm.service.TbUserCouponService tbUserCouponService;

    //进入优惠券管理页
    @RequestMapping("")
    public String show(TbCoupon tbCoupon ){
        return PREFIX+"coupon.html";
    }

    //分页查询优惠券
    @RequestMapping("/list")
    @ResponseBody
    public Object listCouponByMap(@RequestParam(value="names", required=false) String name,@RequestParam(value="active", required=false) Integer active){
        Page<Map<String, Object>> clist = this.couponService.selectCoupons(name,active);
        //OrderWrapper数据传到页面上需要进行一次封装
        Page<Map<String, Object>> warpper = new TbCouponWrapper(clist).wrap();
        return LayuiPageFactory.createPageInfo(warpper);
    }

    //删除滑块
    @RequestMapping("/active")
    @ResponseBody
    public  String  active(Integer id,int active)  {
        //判断是否有用户拥有未过期的该优惠卷
        Integer count= tbUserCouponService.selectUserCouponsByCouponId(id).size();
        if(count!=0 && active==1){
                return "";
        }
        couponService.UpdateActive(id,active);
        return "true";
    }

    //跳转添加页面
    @RequestMapping("/openAddCoupon")
    public String openAddCoupon() {
        return PREFIX+"coupon_add_type.html";
    }

    //跳转编辑页面
    @RequestMapping("/openUpdateCoupon")
    public String goCouponUpdate(@RequestParam("id") Integer id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //获取菜单当前信息，记录日志用
        TbCoupon coupon = this.couponService.selectById(id);
        LogObjectHolder.me().set(coupon);

        return PREFIX+"coupon_modify_type.html";
    }

    //获取回显信息
    @RequestMapping("/getCouponInfo")
    @ResponseBody
    public Object getCouponInfo(@RequestParam Integer id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        TbCoupon coupon = this.couponService.selectById(id);
        Map<String, Object> map = BeanUtil.beanToMap(coupon);
        return ResponseData.success(map);
    }

    @RequestMapping("/editOrAdd")
    @ResponseBody
    public ResponseData editOrAdd(@Valid TbCoupon coupon, BindingResult result) {
        if (coupon.getId() == null) {
            //添加类目/
            coupon.setId(999);
            this.couponService.insertTbCoupon(coupon);
            return SUCCESS_TIP;
        }
        //修改类目
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        couponService.updateById(coupon);
        return SUCCESS_TIP;
    }


}
